import { axiosInstance } from 'src/boot/axios'
import CommonConfig from 'src/assets/js/config'
import _ from 'lodash'
import { loadToken } from 'yz-help'
// import qs from 'qs'
const baseUrl = CommonConfig.machinestatusUrl + '/v1'
const schemaUrl = CommonConfig.schemaUrl + '/v1'
const planningUrl = CommonConfig.planningUrl + '/v1'
const datacenterUrl = CommonConfig.datacenterUrl + '/v1'
const upmsUrl = CommonConfig.upmsUrl + '/v1'
const bmsUrl = CommonConfig.bmsUrl + '/v1'

/**
 * 查询实时状态分布
 * shiftId 班制id
 * workShopId 车间id
 * **/
export function getRealtimeStatus (params, workShopId) {
  const newUrl = `${baseUrl}/workshops/${workShopId}/real_time_status`

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 查询当班状态分布
 * start 班制开始时间
 * workShopId 车间id
 * **/
export function getCurStatus (params) {
  const newUrl = `${baseUrl}/workshops/${params.workshopId}/shift_status_chart`

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}
/**
 * 实时作业状态分布
 * **/
export function getStatisticsState ({ workshopId, lineId }) {
  const newUrl = `${baseUrl}/workshops/${workshopId}/statistics_state`

  return axiosInstance.get(newUrl, { params: { lineId } }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 查询当班机床作业状态分布
 * shiftId 班制id
 * workShopId 车间id
 * **/
export function getCurMachineStatus (params) {
  const newUrl = `${baseUrl}/machine_shiftStatusChart`

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 查询当班停机原因
 * start 开始时间
 * end 结束时间
 * id 车间id
 * **/
export function getStopReason (params, id) {
  const newUrl = `${baseUrl}/workshops/${id}/device_status_durations`

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 查询当班不良统计
 * start 开始时间
 * end 结束时间
 * id 车间id
 * **/
export function getBadChart (params, id, shiftId) {
  const newUrl = `${planningUrl}/workshops/${id}/quality_control_causes/part_bad:statistical`

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 根据车间ID 获取车间在用班制
 * id 车间ID
 * **/
export function getShifts (id) {
  if (_.isNil(id)) {
    return
  }
  const newUrl = `${schemaUrl}/workshop/${id}/shifts/status:use/id`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 列举设备开动率
 * start 开始时间
 * end 结束时间
 * id 车间ID
 * shiftId 班制id
 * **/
export function getDeviceRate (params, id) {
  const newUrl = `${baseUrl}/workshops/${id}/device_operating_rate`

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 消息列表
 * start 开始时间
 * end 结束时间
 * workshopId 车间ID
 * pageSize 定为20
 * **/
export function getMessage (params) {
  const newUrl = `${baseUrl}/messageList`

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 停机原因未填写页
 * @param params
 */
export function getStopNotWrite (params) {
  const newUrl = `${baseUrl}/list_device/stop_causes/not_write`

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 获取验证码
 * @param params
 */
export function getSmsCode (params) {
  const newUrl = `/sms/v1/code`

  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 获取optToken
 * @param phone 电话号码
 * @param smsCode 验证码
 */
export function getOptToken (phone, smsCode) {
  const newUrl = `${datacenterUrl}/account/exp/validateSms?phone=${phone}&smsCode=${smsCode}`

  return axiosInstance.post(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}
export function getCompanies () {
  const newUrl = `${datacenterUrl}/companies/query`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}
/**
 * 提交企业信息
 **/
export function submitCompanies (params) {
  const newUrl = `${datacenterUrl}/companies/submit`

  return axiosInstance.post(newUrl, params, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  }).then((res) => {
    return Promise.resolve(res)
  })
}
/**
 * 更改企业信息
 **/
export function updateCompanies (params) {
  const newUrl = `${datacenterUrl}/companies/update`
  return axiosInstance.post(newUrl, params, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 更新token
 * @param tid
 * @returns {Promise<AxiosResponse<any>>}
 */
export function updateToken (tid) {
  const newUrl = '/v1/update_token'
  return axiosInstance.post(newUrl, null, {
    headers: {
      tid: tid,
      Authorization: 'bearer ' + loadToken()
    }
  }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 获取企业下的角色数量
 * @param params
 */
export function getRolesCount () {
  const newUrl = `${upmsUrl}/roles/count`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 获取企业下的用户数量
 * @returns {Promise<AxiosResponse<any>>}
 */
export function getUsersCount () {
  const newUrl = `${datacenterUrl}/users/count`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 列举租户拥有的全部视图权限 ID-带有服务期限的
 * @param companyId
 * @param clientType
 * @returns {Promise<AxiosResponse<any>>}
 */
export function getCompanyPrivileges (companyId, clientType = 'pc') {
  const newUrl = `${bmsUrl}/companies/${companyId}/view_privileges_date`
  const params = {
    clientType,
    companyId
  }
  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 列举用户拥有的全部快速入口视图权限 ID
 * @param companyId
 */
export function getQuickEntryPrivileges () {
  const newUrl = `${upmsUrl}/users/quick_entry_privileges`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

/**
 * 列举租户拥有的全部视图权限 ID-带有服务期限的
 * @param companyId
 * @param clientType
 * @returns {Promise<AxiosResponse<any>>}
 */
export function setQuickEntryPrivileges (ids) {
  const newUrl = `${upmsUrl}/users/quick_entry_privileges`
  return axiosInstance.post(newUrl, ids).then((res) => {
    return Promise.resolve(res)
  })
}
